<?php

namespace app\api\Logic\Course;

use app\api\model\CampusCourseManagementModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

class ManagementLogic
{
    /**
     * 专项课详情
     * @param $managementId
     * @return array
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function managementInfo($managementId){
        $info = CampusCourseManagementModel::where('id',$managementId)
            ->field('id,name,class_type,hour,hour_status,monthly,monthly_status,day,per_diem_status')
            ->find();
        $info = objToArray($info) ?? [];

        //按课时收费
        if($info['hour_status'] == 1){
            $info['hour'] = unserialize($info['hour']);
        }
        //按月收费
        if($info['monthly_status'] == 1){
            $info['monthly'] = unserialize($info['monthly']);
        }
        //按天收费
        if($info['per_diem_status'] == 1){
            $info['day'] = unserialize($info['day']);
        }

        return $info;
    }

    /**
     * 获取课程列表
     * @param $params
     * @param string $field
     * @param false $isLimit
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getList($params,$field = "*",$isLimit = false){
        $query = CampusCourseManagementModel::field($field)
            ->where('shop_id',$params['shop_id']);

        //根据课程ID序列筛选
        if(isset($params['courseIds']) && !empty($params['courseIds'])){
            $courseIds = is_array($params['courseIds']) ? $params['courseIds'] : explode(',',$params['courseIds']);
            $query = $query->whereIn('id',$courseIds);
        }

        //分页
        if($isLimit){
            $list = $query->paginate(PAGE_SIZE);
            $res = objToArray($list) ?? [];
        }else{
            $list = $query->select();
            $res['data'] = objToArray($list) ?? [];
        }

        return $res;
    }
}
